﻿//======================================================
//==     (c)2013  OpenLayers.cn inc by 飞龙在天 v1.0  ==
//==          Forum:bbs.OpenLayers.cn                 ==
//==         Website:www.OpenLayers.cn                ==
//== 开源旗舰版见 淘宝: http://giscenter.taobao.com   ==
//======================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace Common
{
    public class SaveImg
    {
        private static bool SaveBinaryFile(WebResponse response, string FileName)
        {
            bool flag = true;
            byte[] buffer = new byte[0x400];//16384 
            int num = FileName.LastIndexOf('\\');
            string path = FileName.Substring(0, num + 1);
            try
            {
                int num2;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                if (System.IO.File.Exists(FileName))
                {
                    System.IO.File.Delete(FileName);
                }
                Stream stream = System.IO.File.Create(FileName);
                Stream responseStream = response.GetResponseStream();
                do
                {
                    num2 = responseStream.Read(buffer, 0, buffer.Length);
                    if (num2 > 0)
                    {
                        stream.Write(buffer, 0, num2);
                    }
                }
                while (num2 > 0);
                stream.Close();
                responseStream.Close();
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        public static string SavePhotoFromUrl(string FileName, string Url)
        {
            bool flag = false;
            WebResponse response = null;
            Stream responseStream = null;
            string str = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)";
                request.Method = "GET";
                response = request.GetResponse();
                responseStream = response.GetResponseStream();
                if (response.ContentType.ToLower().StartsWith("image/"))
                {
                    str = response.ContentType.ToLower().Replace("image/", "");
                    if (str == "jpeg")
                    {
                        str = "jpg";
                    }
                    FileName = FileName + "." + str;
                    flag = SaveBinaryFile(response, FileName);
                    return str;
                }
                Console.WriteLine(Url);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return str;
        }
    }
}
